-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
52 lines (46 loc) · 1.41 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
using namespace std;
// DFS function to explore a connected component
void dfs(int vertex, const vector<vector<int>> &graph, vector<bool> &visited, vector<int> &component) {
visited[vertex] = true;
component.push_back(vertex);
for (int neighbor : graph[vertex]) {
if (!visited[neighbor]) {
dfs(neighbor, graph, visited, component);
}
}
}
int main() {
int V, E;
cout << "Enter the number of vertices: ";
cin >> V;
cout << "Enter the number of edges: ";
cin >> E;
// Create an adjacency list for the graph
vector<vector<int>> graph(V);
cout << "Enter the edges (u v) for each edge (0-indexed):" << endl;
for (int i = 0; i < E; i++) {
int u, v;
cin >> u >> v;
// Since the graph is undirected, add edges in both directions.
graph[u].push_back(v);
graph[v].push_back(u);
}
vector<bool> visited(V, false);
int componentCount = 0;
cout << "Connected Components:" << endl;
for (int i = 0; i < V; i++) {
if (!visited[i]) {
vector<int> component;
dfs(i, graph, visited, component);
componentCount++;
cout << "Component " << componentCount << ": ";
for (int node : component) {
cout << node << " ";
}
cout << endl;
}
}
return 0;
}